package com.cx.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cx.mall.common.model.order.OrderInfo;
import com.cx.mall.common.model.order.OrderQuery;
import com.cx.mall.order.mapper.OrderInfoMapper;
import com.cx.mall.order.service.IOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author lyt
 * @since 2025-07-14
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    /**
     * 根据用户ID查询订单信息
     *
     * @param id 用户ID
     * @return 订单信息列表
     */
    @Override
    public List<OrderInfo> listByUserId(Long id) {
        // 查询订单信息
        LambdaQueryChainWrapper<OrderInfo> queryChainWrapper =
                new LambdaQueryChainWrapper<>(baseMapper);
        List<OrderInfo> list = queryChainWrapper.eq(OrderInfo::getUserId, id).list();
        return list;
    }

    /**
     * 根据订单状态查询订单列表
     *
     * @param status 订单状态
     * @return 订单信息列表
     **/
    @Override
    public List<OrderInfo> listByOrderState(Long id, Integer status) {
        // 1.查询订单信息
        LambdaQueryChainWrapper<OrderInfo> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        List<OrderInfo> list;
        //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        if (status != null) {
            if (status < 0 || status > 5) {
                throw new RuntimeException("订单状态错误");
            } else {
                list = queryChainWrapper.eq(OrderInfo::getStatus, status).list();
            }
        } else {
            // status为null时查询所有订单
            list = queryChainWrapper.list();
        }
        return list;
    }

    @Override
    public Page page(OrderQuery query) {
        //构建条件构造器
        LambdaQueryChainWrapper<OrderInfo> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        //拼接条件
        if (query.getUserId() != null) {
            //按用户id查询
            wrapper.eq(OrderInfo::getUserId, query.getUserId());
        }
        if (query.getOrderId() != null) {
            //按订单id查询
            wrapper.eq(OrderInfo::getOrderId, query.getOrderId());
        }
        if (query.getStatus() != null) {
            //按订单状态查询
            wrapper.eq(OrderInfo::getStatus, query.getStatus());
        }
        if (query.getStartTime() != null && query.getEndTime() != null) {
            wrapper.between(OrderInfo::getCreateTime, query.getStartTime(), query.getEndTime());
        } else if (query.getStartTime() != null) {
            //按下单时间
            wrapper.ge(OrderInfo::getCreateTime, query.getStartTime());
            //wrapper.between(OrderInfo::getCreateTime, query.getStartTime(),"9999-12-31");
        }
        else if (query.getEndTime() != null) {
            //按结束时间
            wrapper.le(OrderInfo::getCreateTime, query.getEndTime());
            //wrapper.between(OrderInfo::getCreateTime,"1970-01-01", query.getEndTime());
        }
        //选择列
        wrapper.select(OrderInfo::getOrderId, OrderInfo::getCreateTime, OrderInfo::getTotalAmount);
        Page page = new Page<>();
        if (query.getCurrent() != null){
            page.setCurrent(query.getCurrent());
        }
        if (query.getSize() != null){
            page.setSize(query.getSize());
        }
        return page(page,wrapper);
    }


    /**
     * 更新订单状态
     */
    @Override
    @Transactional
    public boolean updateStatus(Long orderId, Integer status) {
        LambdaUpdateWrapper<OrderInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrderInfo::getOrderId, orderId)
                .set(OrderInfo::getStatus, status)
                .set(OrderInfo::getUpdateTime, new Date()); // 更新时间
        return baseMapper.update(null, updateWrapper) > 0;
    }

}